package cn.tsa.share.jdbc.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.Map;

@Configuration
public class DataSourceHealthConfig extends DataSourceHealthContributorAutoConfiguration {

	private final String defaultQuery = "SELECT 1";

	public DataSourceHealthConfig(Map<String, DataSource> dataSources, ObjectProvider<DataSourcePoolMetadataProvider> metadataProviders) {
		super(dataSources, metadataProviders);
	}

	@Override
	protected AbstractHealthIndicator createIndicator(DataSource source) {
		DataSourceHealthIndicator indicator = (DataSourceHealthIndicator) super.createIndicator(source);
		if (!StringUtils.hasText(indicator.getQuery())) {
			indicator.setQuery(defaultQuery);
		}
		return indicator;
	}
}
